# 检查账户是否具有杠杆交易权限的代码示例
def check_leverage_permission(api_key, api_secret):
    """检查账户杠杆交易权限"""
    import requests
    import hmac
    import hashlib
    import time

    base_url = "https://api.binance.com"
    endpoint = "/sapi/v1/margin/account"
    timestamp = int(time.time() * 1000)

    # 获取服务器时间
    try:
        time_resp = requests.get(f"{base_url}/api/v3/time")
        timestamp = time_resp.json()['serverTime']
    except:
        pass

    params = f"timestamp={timestamp}&recvWindow=5000"
    signature = hmac.new(
        api_secret.encode('utf-8'),
        params.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()

    url = f"{base_url}{endpoint}?{params}&signature={signature}"
    headers = {"X-MBX-APIKEY": api_key}

    try:
        response = requests.get(url, headers=headers, timeout=10)
        if response.status_code == 200:
            print("✅ 账户已开通杠杆交易功能")
            return True
        else:
            print(f"❌ 杠杆交易未开通或权限不足: {response.status_code}")
            return False
    except Exception as e:
        print(f"❌ 检查失败: {e}")
        return False


API_KEY = "oMlYVBgANplqH7XilK0rzJ47r7p1R3MWoywVNBYOGvj5UHYXebQnxZjX9kLpdd5V"
API_SECRET = "8587vRjV45QsPPXhlrn3vcBdUwQJFA6HIBqfaVbJO7UqhhqTcVrQCR4GEO6w4OtN"
check_leverage_permission(API_KEY, API_SECRET)